import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.bytes.ByteArrayEncoder;
import io.netty.handler.timeout.IdleStateHandler;
import org.nettyServer.server.IdleHandler;
import org.nettyServer.server.TCPBaseMessageDecoder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Component
public class ClientInit extends ChannelInitializer {

    @Resource
    private ClientHandler clientHandler;



    @Override
    protected void initChannel(Channel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        //心跳检测
        pipeline.addLast(new IdleStateHandler(0,0 , 0, TimeUnit.SECONDS));
//        pipeline.addLast(new IdleHandler());
        //字节数组编码
        pipeline.addLast(new ByteArrayEncoder());
        //字节数组解码（重写类，如不需要可使用默认）
        pipeline.addLast(new TCPBaseMessageDecoder());
        //客户端的逻辑
        pipeline.addLast("handler", clientHandler);
    }
}
